package com.autumn.annotations.leetcode.two;

import com.autumn.annotations.leetcode.basic.ListNode;

/**
 * 项目名称：com.autumn.xa
 * 类 名 称：Demo
 * 类 描 述：TODO
 * 创建时间：2021/9/15 9:10 下午
 * 创 建 人：yeachrom
 */
public class Demo {



    public static ListNode removeNthFromEnd(ListNode head, int n) {
        // write your code here.

        int all = 0;
        ListNode check = head;
        do {
            all++;
            check = check.next;
        } while (check != null);

        int that = all - n + 1;

        ListNode now = head;
        ListNode before = null;
        for (int i = 1; i <= that; i++) {
            if (now == null) {
                break;
            }
            if (i == that){
                before.next = now.next;
                break;
            }
            before = now;
            now = now.next;
        }
        return head;
    }

    public static void main(String[] args) {
        ListNode n5 = new ListNode(5);
        ListNode n4 = new ListNode(4, n5);
        ListNode n3 = new ListNode(3, n4);
        ListNode n2 = new ListNode(2, n3);
        ListNode n1 = new ListNode(1, n2);
        ListNode n = removeNthFromEnd(n1, 2);
        System.out.println(n);
    }

}
